package mcommand;

import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JPanel;

import mcommand.MainPanel.MainPanelState;

/**
 * Adds the buttons for the start menu and implements what they should do.
 *
 * @author Marcel, Christian, Klaus
 */
@SuppressWarnings("serial")
public class MenuPanel extends JPanel {
    // sets a dark look for the buttons
    private static final Color BUTTON_BACKGROUND = Color.BLACK;
    private static final Color BUTTON_FONTCOLOR = Color.WHITE;

    // defines a variable which holds where to find the button sound
    private static final String PATH_SOUND_BUTTON = "assets/button.wav";

    // defines a soundHandler for the button sound
    private final SoundHandler buttonSound;

    // initiates a variable for a StartMenu
    StartMenu parent;

    // initiates a variable for a MainPanel
    MainPanel mainPanel;

    // defines new buttons
    JButton buttonStart;
    JButton buttonHighscore;
    JButton buttonHelp;
    JButton buttonExit;

    // sets the variables with values for the GridLayout
    private final int cellRow = 4;
    private final int cellColumns = 1;
    private final int cellHgap = 5;
    private final int cellVgap = 5;

    /**
     * Sets how the menu panel should look.
     * For example how the button should look and
     * how the will be placed.
     * Sets also parent and mainPanel variables with
     * the parameters and creates a new SoundHandler
     * for the button sound.
     * 
     * @param parent parent
     * @param mainPanel mainPanel
     */
    public MenuPanel(final StartMenu parent, final MainPanel mainPanel) {
        // sets the parent to this parent
        this.parent = parent;

        // sets the MainPanel to this MainPanel
        this.mainPanel = mainPanel;

        // sets a new SoundHandler for the button sound
        buttonSound = new SoundHandler(PATH_SOUND_BUTTON);

        // sets the Layout to a GridLayout
        setLayout(new GridLayout(cellRow, cellColumns, cellHgap, cellVgap));

        // sets the Background to be black
        setBackground(Color.BLACK);

        // sets up a new button to start game, adds a StartListener,
        // applies the dark layout and adds the button
        buttonStart = new JButton("Start");
        buttonStart.addActionListener(new StartListener());
        buttonStart.setForeground(BUTTON_FONTCOLOR);
        buttonStart.setBackground(BUTTON_BACKGROUND);
        add(buttonStart);

        // sets up a new button to show highscore, adds a HighscoreListener,
        // applies the dark layout and adds the button
        buttonHighscore = new JButton("Highscore");
        buttonHighscore.addActionListener(new HighscoreListener());
        buttonHighscore.setForeground(BUTTON_FONTCOLOR);
        buttonHighscore.setBackground(BUTTON_BACKGROUND);
        add(buttonHighscore);

        // sets up a new button to show help, adds a HelpListener,
        // applies the dark layout and adds the button
        buttonHelp = new JButton("Help");
        buttonHelp.addActionListener(new HelpListener());
        buttonHelp.setForeground(BUTTON_FONTCOLOR);
        buttonHelp.setBackground(BUTTON_BACKGROUND);
        add(buttonHelp);

        // sets up a new button to exit program, adds a ExitListener,
        // applies the dark layout and adds the button
        buttonExit = new JButton("Exit");
        buttonExit.addActionListener(new ExitListener());
        buttonExit.setForeground(BUTTON_FONTCOLOR);
        buttonExit.setBackground(BUTTON_BACKGROUND);
        add(buttonExit);
    }

    /**
     * Plays the button sound and calls the startGame Method of the class
     * StartMenu.
     * 
     */
    private class StartListener implements ActionListener {
        /**
         * If a action is performed, the button sound plays once
         * and the startGame method of the parent is called.
         * 
         * @param e e
         */
        public void actionPerformed(final ActionEvent e) {
            buttonSound.playSoundOnce();
            parent.startGame();
        }
    }

    /**
     * Shows and hides the highscore.
     * 
     */
    private class HighscoreListener implements ActionListener {
        /**
         * If a action is performed, it show
         * and hides the highscore.
         * 
         * @param e e
         */
        public void actionPerformed(final ActionEvent e) {
            // plays the button sound
            buttonSound.playSoundOnce();

            // if the MainPanelState is not HIGHSCORE (hides the highscore)
            if (!(mainPanel.getState() == MainPanelState.HIGHSCORE)) {
                // changes the text of the buttons
                buttonHighscore.setText("Back");
                buttonHelp.setText("Help");
                // sets the MainPanelState to HIGHSCORE
                mainPanel.setState(MainPanelState.HIGHSCORE);

                // if the MainPanelState is HIGHSCORE (shows the highscore)
            } else {
                // changes the text of the buttons
                buttonHighscore.setText("Highscore");
                buttonHelp.setText("Help");
                // sets the MainPanelState to SPLASH
                mainPanel.setState(MainPanelState.SPLASH);
            }
        }
    }
    
    /**
     * Shows and hides the instructions image from the help menu.
     *
     */
    private class HelpListener implements ActionListener {
        /**
         * If a action is performed it shows and hides the
         * instructions image from the help menu.
         * 
         * @param e e
         */
        public void actionPerformed(final ActionEvent e) {
            // plays the button sound
            buttonSound.playSoundOnce();

            // if the MainPanelState is not HELP (shows the help)
            if (!(mainPanel.getState() == MainPanelState.HELP)) {
                // changes the text of the buttons
                buttonHelp.setText("Back");
                buttonHighscore.setText("Highscore");
                // sets the MainPanelState to HELP
                mainPanel.setState(MainPanelState.HELP);

                // if the MainPanelState is HELP (hides the help)
            } else {
                // changes the text of the buttons
                buttonHelp.setText("Help");
                buttonHighscore.setText("Highscore");
                // sets the MainPanelState to SPLASH
                mainPanel.setState(MainPanelState.SPLASH);
            }
        }
    }

    /**
     * Closes the program completely.
     */
    private class ExitListener implements ActionListener {
        /**
         * If a action is performed, it closes the window
         * completely.
         * 
         * @param e e
         */
        public void actionPerformed(final ActionEvent e) {
            System.exit(0);

        }
    }

    /**
     * Sets the Highscore button to back when back from game
     * and the highscore is shown.
     */
    public final void backFromGame() {
        buttonHighscore.setText("Back");
    }

}
